/*
 * 作者：姚俊
 * 功能：导师成员信息报表
 * 日期：2015-05-09
 */
package admin.databaseToExcel;

import java.io.File;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import teacher.bean.Teacher;
import teacher.service.teacher;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class PullTeacherToExcel {
	
	/**
	 * @category 从数据库当中导入导师表单
	 * @author yaojun
	 */
	public static void teacherExcel()
	{
        try {
            WritableWorkbook wwb = null; 
            String uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");
            System.out.println("全程的路径为:"+ServletActionContext.getServletContext()
					.getRealPath(""));
            
            File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
               // 创建可写入的Excel工作簿
              String fileName = uploadPath+"/"+"teacher.xls";
//            String fileName="D://teachers.xls";
               File file=new File(fileName);
               if (!file.exists()) {
                   file.createNewFile();
               }
               //以fileName为文件名来创建一个Workbook
               wwb = Workbook.createWorkbook(file);
               // 创建工作表
               WritableSheet ws = wwb.createSheet("Test Shee 1", 0);             
               //查询数据库中所有的数据
               List<Teacher> teachers= new teacher().SelectAllTeacher();
               //要插入到的Excel表格的行号，默认从0开始
               Label tid= new Label(0, 0, "编号");//表示第
               Label tname= new Label(1, 0, "姓名");//表示第
               Label tduty= new Label(2, 0, "专业技术职务");
               Label tdegree= new Label(3, 0, "学位");   
               Label ttopSchool= new Label(4, 0, "最高学历毕业学校");
               Label ttopMajor= new Label(5, 0, "最高学历毕业专业");
               ws.addCell(tid);
               ws.addCell(tname);
               ws.addCell(tduty);
               ws.addCell(tdegree);
               ws.addCell(ttopSchool);
               ws.addCell(ttopMajor);
               

               for (int i = 0; i < teachers.size(); i++) {  
            	   String degree=null;
            	   Label ttid= new Label(0, i+1, teachers.get(i).getTeaId()+"");
                   Label ttname= new Label(1, i+1, teachers.get(i).getName()+"");
                   Label ttduty= new Label(2, i+1, teachers.get(i).getDuty());
                   if(teachers.get(i).getDegree()==0){
                	   degree="博士";
                   }else if(teachers.get(i).getDegree()==1){
                	   degree="硕士";
                   }else{
                	   degree="学士";
                   }
                   Label ttdegree= new Label(3, i+1, degree+""); 
                   Label tttopSchool= new Label(4, i+1, teachers.get(i).getTopSchool()+"");  
                   Label tttopMajor= new Label(5, i+1, teachers.get(i).getTopMajor()+"");  
                   
                   
                   ws.addCell(ttid);
                   ws.addCell(ttname);
                   ws.addCell(ttduty);
                   ws.addCell(ttdegree);          
                   ws.addCell(tttopSchool);
                   ws.addCell(tttopMajor);
               }  
              //写进文档
               wwb.write();
              // 关闭Excel工作簿对象
               wwb.close();     
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

}
